package csv_parser;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;

public class CSVToMatrix {

	public static void writeToFiles(String trainingFile, String validationFile,
			ArrayList<String> data, ArrayList<String> plottableData) {
		Collections.shuffle(data);
		System.out.println(data.size());
		int n = 3 * data.size() / 4;
		int m = 1 * data.size() / 4;
		System.out.println(n + ", " + m);
		System.out.println(data.get(0).split(",").length);
		try {
			@SuppressWarnings("resource")
			PrintWriter writer = new PrintWriter(new File(trainingFile));
			for (int i = 0; i < n; i++) {
				writer.println(data.get(i));
			}
			writer.close();

			PrintWriter writer1 = new PrintWriter(new File(validationFile));
			for (int i = n; i < data.size(); i++) {
				writer1.println(data.get(i));
			}
			writer1.close();

			PrintWriter writer2 = new PrintWriter(new File("data.txt"));
			Collections.shuffle(plottableData);
			for (int i = 0; i < plottableData.size() / 4; i++) {
				writer2.println(plottableData.get(i));
			}
			writer2.close();

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

	static float[] max = new float[6];
	static float[][] matrix = new float[1050][6];
	static int[] types = new int[1050];
	static float[] mean = new float[6];
	// d_max 37
	// q_d_max 38
	// alpha_f_min 2
	// f_min 64
	// alpha_f_max 100
	// f_max 81
	static float[] importance_factor = new float[6];
	static float[] std = new float[6];
	static float[] min = new float[6];
	static int matrix_i = 0;
	static int count = 0;

	static {
		for (int i = 0; i < min.length; i++)
			min[i] = Float.MAX_VALUE;

		importance_factor[0] = 1f;
		importance_factor[1] = 1f;
		importance_factor[2] = 1.1f;
		importance_factor[3] = 1f;
		importance_factor[4] = 0.1f;
		importance_factor[5] = 0.25f;

	}

	static void doPolynomial(String fileName, int type, ArrayList<String> data,
			ArrayList<String> plottableData) {
		Scanner sc;

		try {
			sc = new Scanner(new File(fileName));
			boolean start = false;
			int j = 0;
			float d_max = 0;
			float f_max = 0;
			float alpha_f_max = 0;
			float q_d_max = 0;
			float f_min = Float.MAX_VALUE;
			float alpha_f_min = 0;
			int size = 0;

			while (sc.hasNext() && size < 290) {

				String[] line = sc.nextLine().split(";");
				if (!start && line.length > 0 && line[0].equals("-10")) {
					start = true;
					d_max = 0;
					f_max = 0;
					alpha_f_max = 0;
					q_d_max = 0;
					f_min = Float.MAX_VALUE;
					alpha_f_min = 0;
					j = 0;
				}
				float[] nums = new float[6];
				if (j < 84 && start) {
					for (int i = 0; i < 6; i++) {
						if (!line[i].equals("?")) {
							String temp = line[i].replace(",", ".");
							float num = Float.parseFloat(temp);
							nums[i] = num;
							if (i == 1 && num > d_max) {
								d_max = num;
								q_d_max = nums[0];
							}

							if (i == 5) {
								if (num < f_min) {
									f_min = num;
									alpha_f_min = nums[4];
								}
								if (num > f_max) {
									f_max = num;
									alpha_f_max = nums[4];
								}
							}

						}
					}
				}
				if (start && line[0].equals("10,75")) {
					count++;
					StringBuilder builder = new StringBuilder();
					matrix[matrix_i][0] = d_max;
					matrix[matrix_i][1] = q_d_max;
					matrix[matrix_i][2] = f_min;
					matrix[matrix_i][3] = alpha_f_min;
					matrix[matrix_i][4] = f_max;
					matrix[matrix_i][5] = alpha_f_max;

					mean[0] += d_max;
					mean[1] += q_d_max;
					mean[2] += f_min;
					mean[3] += alpha_f_min;
					mean[4] += f_max;
					mean[5] += alpha_f_max;

					min[0] = Math.min(min[0], d_max);
					min[1] = Math.min(min[1], q_d_max);
					min[2] = Math.min(min[2], f_min);
					min[3] = Math.min(min[3], alpha_f_min);
					min[4] = Math.min(min[4], f_max);
					min[5] = Math.min(min[5], alpha_f_max);

					max[0] = Math.max(max[0], d_max);
					max[1] = Math.max(max[1], q_d_max);
					max[2] = Math.max(max[2], f_min);
					max[3] = Math.max(max[3], alpha_f_min);
					max[4] = Math.max(max[4], f_max);
					max[5] = Math.max(max[5], alpha_f_max);

					types[matrix_i] = type;
				
					start = false;
			
					matrix_i++;
					size++;
				}
				j++;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public static boolean isCorrect(File file, Message msg) {
		try {
			@SuppressWarnings("resource")
			Scanner sc = new Scanner(file);
			boolean start = false;
			int j = 0;

			while (sc.hasNext()) {

				String[] line = sc.nextLine().split(";");
				if (!start && line.length > 0 && line[0].equals("-10")) {
					start = true;
					j = 0;
				}
				float[] nums = new float[6];
				if (j < 84 && start) {
					for (int i = 0; i < 6; i++) {
						if (!line[i].equals("?")) {
							String temp = line[i].replace(",", ".");
							float num = Float.parseFloat(temp);
							nums[i] = num;
						}
					}
				}
				j++;
			}
		} catch (Exception e) {
			msg.message = "Nastala je greska, pokusajte s drugim fajlom";
			e.printStackTrace();
			return false;
		}

		msg.message = "All good";
		return true;
	}

	public static void readData(String fileName, int type) {
		Scanner sc;
		System.out.println("Toi toi");
		try {
			sc = new Scanner(new File(fileName));
			// read header
			sc.nextLine();
			System.out.println("reading file " + fileName);
			while (sc.hasNext()) {
				String[] line = sc.nextLine().replace(",", ".").split(";");
				if (line.length < 7)
					continue;
				matrix[matrix_i][0] = Float.parseFloat(line[0]);
				matrix[matrix_i][1] = Float.parseFloat(line[1]);
				matrix[matrix_i][2] = Float.parseFloat(line[2]);
				matrix[matrix_i][3] = Float.parseFloat(line[3]);
				matrix[matrix_i][4] = Float.parseFloat(line[4]);
				matrix[matrix_i][5] = Float.parseFloat(line[5]);
				types[matrix_i] = type;

				mean[0] += matrix[matrix_i][0];
				mean[1] += matrix[matrix_i][1];
				mean[2] += matrix[matrix_i][2];
				mean[3] += matrix[matrix_i][3];
				mean[4] += matrix[matrix_i][4];
				mean[5] += matrix[matrix_i][5];

				min[0] = Math.min(min[0], matrix[matrix_i][0]);
				min[1] = Math.min(min[1], matrix[matrix_i][1]);
				min[2] = Math.min(min[2], matrix[matrix_i][2]);
				min[3] = Math.min(min[3], matrix[matrix_i][3]);
				min[4] = Math.min(min[4], matrix[matrix_i][4]);
				min[5] = Math.min(min[5], matrix[matrix_i][5]);

				max[0] = Math.max(max[0], matrix[matrix_i][0]);
				max[1] = Math.max(max[1], matrix[matrix_i][1]);
				max[2] = Math.max(max[2], matrix[matrix_i][2]);
				max[3] = Math.max(max[3], matrix[matrix_i][3]);
				max[4] = Math.max(max[4], matrix[matrix_i][4]);
				max[5] = Math.max(max[5], matrix[matrix_i][5]);

				matrix_i++;
				count++;
			}
		} catch (FileNotFoundException ex) {
			ex.printStackTrace();

		}

	}

	static void normalize(ArrayList<String> data,
			ArrayList<String> plottableData) {
		for (int i = 0; i < mean.length; i++) {
			mean[i] /= count;
			std[i] = max[i] - min[i];
		}

		for (int i = 0; i < 6; i++) {
			System.out.println(mean[i] + " " + std[i]);
		}

		for (int i = 0; i < count; i++) {
			System.out.println(Arrays.toString(matrix[i]) + " " + types[i]);
			for (int jz = 0; jz < 6; jz++) {
				matrix[i][jz] = (matrix[i][jz] - mean[jz])
						/ (importance_factor[jz] * std[jz]);
			}
			System.out.println(Arrays.toString(matrix[i]) + " " + types[i]);

		}

		System.out.println("count = " + count);
		for (int i = 0; i < count; i++) {
			StringBuilder builder = new StringBuilder();
			for (int j = 0; j < 6; j++) {
				builder.append(matrix[i][j] + ",");
			}
			
			int num = 0;
			for (int j = 0; j < 6; j++) {
				for (int k = j; k < 6; k++) {
					builder.append(matrix[i][j] * matrix[i][k] + ",");
					num++;
				}
			}

			builder.append(types[i]);
			data.add(builder.toString());

			StringBuilder sbuilder = new StringBuilder();
			sbuilder.append(matrix[i][4] + ",");
			sbuilder.append(matrix[i][5] + ",");
			sbuilder.append(types[i]);
			plottableData.add(sbuilder.toString());
		}
	}

	public static void normalize(float[] data) {

		for (int i = 0; i < data.length; i++) {
			data[i] = (data[i] - mean[i]) / (importance_factor[i] * std[i]);
		}
	}

	public static float[] createInput(float[] data) {
		float[] result = new float[27];
		int count = 0;
		for (int i = 0; i < data.length; i++) {
			result[i] = data[i];
			count++;
		}

		for (int j = 0; j < 6; j++) {
			for (int k = j; k < 6; k++) {
				result[count++] = data[j] * data[k];
			}
		}

		return result;
	}

	public static StringBuilder prepareData(float[] data) {
		StringBuilder builder = new StringBuilder();
		for(int i = 0; i < data.length; i++){
			if(i > 0)
				builder.append(",");
			builder.append(data[i]);
		}
		return builder;
	}

}
